package com.cup.common.response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 响应体结构
 * @author alan.zhao
 */
public class ResponseData<T> {
	static final Logger log = LoggerFactory.getLogger(ResponseData.class);;

	private String flg = ResponseCode.SUCCESS_FLAG;

	private String msg = "";

	private T data;

	public void init(T data){
		this.data = data;
	}

	public void init(String flg, String msg){
		this.flg = flg;
		this.msg = msg;
	}

	@JSONField(ordinal = 0)
	public String getFlg() {
		return flg;
	}

	public void setFlg(String flg) {
		this.flg = flg;
	}

	@JSONField(ordinal = 1)
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@JSONField(ordinal = 2)
	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
	public String toResponse() {
		return JSON.toJSONString(this, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
	}

	/**
	 * 调用当前对象的toResponse()，输出当前对象的字符串形式。
	 * 此方法一般用于使用其他API直接输出ResponseBody对象
	 */
	@Override
	public String toString() {
		return this.toResponse();
	}
}
